package com.lagou.edu.controller;

import com.alibaba.druid.util.StringUtils;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/showResume")
    public ModelAndView showResume() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("resume");
        return modelAndView;
    }
    @RequestMapping("/editResume")
    @ResponseBody
    public ModelAndView handleRequest(Long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        if (id == null || StringUtils.isEmpty("" + id)) {
            modelAndView.addObject("resume", new Resume());
        } else {
            Resume resume = resumeService.findById(id);
            modelAndView.addObject("resume", resume);
        }
        modelAndView.setViewName("resume_update");
        return modelAndView;
    }
    @RequestMapping(value = "/findAll")
    @ResponseBody
    public List<Resume> findAll() {
        List<Resume> resumeList = resumeService.findAll();
        return resumeList;
    }
    @RequestMapping("/updateResume")
    public ModelAndView updateResume(Resume resume) {
        ModelAndView modelAndView = new ModelAndView();
        resumeService.save(resume);
        modelAndView.setViewName("resume");
        return modelAndView;
    }
    @RequestMapping("/deleteResume")
    public ModelAndView deleteResume(Long id) {
        ModelAndView modelAndView = new ModelAndView();
        resumeService.deleteById(id);
        modelAndView.setViewName("resume");
        return modelAndView;
    }

}
